package com.xinxin.socket.sample.webserver;

import com.xinxin.socket.proto.Login;
import lombok.extern.slf4j.Slf4j;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.StreamingOutput;
import java.io.InputStream;

@Slf4j
@Path("server")
public class LoginCgi {

    @POST()
    @Consumes("application/octet-stream")
    @Produces("application/octet-stream")
    @Path("login")
    public Response loginMars(InputStream in) {
        try {
            Login.LoginRequest request = Login.LoginRequest.parseFrom(in);// 接受socket传递过来的数据
            //校验key值 失败了 关闭socket 成功放行
//            CommonResponseVO<UserResponseVo> res = authService.loginByKey(request.getKey(), 1);
//            Integer userId = tokenService.getTokenUserId(key);
//            User user = redisService.getUser(userId);
//            if (null == user || StringUtils.isBlank(user.getToken())) {
//                return CommonResponseVO.error(MsgEnum.getMessage(language, MsgEnum.AUTH_FAILED.name()));
//            }
//            if (!key.equals(user.getToken())) {
//                return CommonResponseVO.error(MsgEnum.getMessage(language, MsgEnum.KEY_OVERDUE.name()));
//            }
//            return CommonResponseVO.success(response(user));
            int retCode = "success".equals(request.getKey()) ?
                    Login.LoginResponse.Code.CODE_OK_VALUE : Login.LoginResponse.Code.CODE_ERR_VALUE;
//            String Msg= res.getMsg();
//            if(res.getCode().equals("200")){
//                retCode=Login.LoginResponse.Code.CODE_OK_VALUE;//返回的状态码为0 成功的状态
//            }
            Login.LoginResponse response = Login.LoginResponse.newBuilder()
                    .setCode(retCode)
                    .setMsg("测试登录")
                    .setUserId(request.getUserId())
                    .build();
            StreamingOutput stream = response::writeTo;
            return Response.ok(stream).build();
        } catch (Exception e) {
            log.error(e.getMessage());
        }
        return null;
    }
}
